package com.free.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.free.ui.R;

/**
 * @author nanbeijiang fengjr loading 画面
 */
public class FengjrLoadingView extends FrameLayout {
	private Animation clockwiseAnimation = null;
	private Animation counterclockwiseAnimation = null;

	ImageView closeWiseImg;
	ImageView counterCloseWiseImg;

	public FengjrLoadingView(Context context) {
		super(context);
		initLoadingView();
	}

	public FengjrLoadingView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initLoadingView();
	}

	public FengjrLoadingView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initLoadingView();
	}

	private void initLoadingView() {
		// log 背景设置
		ImageView logImg = new ImageView(getContext());
		logImg.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		logImg.setImageResource(R.drawable.loading_logo);

		// 顺时针 转向图标
		// ImageView closeWiseImg = new ImageView(getContext());
		closeWiseImg = new ImageView(getContext());
		// closeWiseImg.setLayoutParams(new LayoutParams(
		// LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

		closeWiseImg.setLayoutParams(new LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		closeWiseImg.setImageResource(R.drawable.closewise);

		// 你是指转向图标
		// ImageView counterCloseWiseImg = new ImageView(getContext());
		counterCloseWiseImg = new ImageView(getContext());
		counterCloseWiseImg.setLayoutParams(new LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		counterCloseWiseImg.setImageResource(R.drawable.counterclosewise);

		// 添加view
		addView(logImg);
		addView(closeWiseImg);
		addView(counterCloseWiseImg);

		// 构建顺时针 逆时针动画
		clockwiseAnimation = AnimationUtils.loadAnimation(getContext(),
				R.anim.clockwise);
		counterclockwiseAnimation = AnimationUtils.loadAnimation(getContext(),
				R.anim.counterclockwise);

		// // 启动动画
		closeWiseImg.setLayerType(View.LAYER_TYPE_HARDWARE, null);
		counterCloseWiseImg.setLayerType(View.LAYER_TYPE_HARDWARE, null);
		closeWiseImg.startAnimation(clockwiseAnimation);
		counterCloseWiseImg.startAnimation(counterclockwiseAnimation);

	}

	public void startAnimation() {
		closeWiseImg.startAnimation(clockwiseAnimation);
		counterCloseWiseImg.startAnimation(counterclockwiseAnimation);
	}

	public void stopAnimation() {
		closeWiseImg.clearAnimation();
		counterCloseWiseImg.clearAnimation();
		clockwiseAnimation.cancel();
		counterclockwiseAnimation.cancel();
	}

}
